package cn.q3c.download.commons;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import cn.q3c.download.base.BaseSpider;

public class DownloadUtils {

	/**
	 * 项目保存目录
	 * 
	 * @param project
	 * @return
	 */
	public static String getProjectSavePath(String projectName) {
		String retPath = Configuration.getInstance().getStorePath();
		return retPath
				+ (StringUtils.endsWith(retPath, File.separator) ? ""
						: File.separator) + projectName + File.separator
				+ "data" + File.separator;
	}

	/**
	 * 项目配置文件保存目录
	 * 
	 * @param project
	 * @return
	 */
	public static String getProjectConfigPath(String projectName) {
		String retPath = getProjectSavePath(projectName);
		return StringUtils.replace(retPath, projectName + File.separator + "data" + File.separator,
				projectName + File.separator + "config" + File.separator);
	}


	public static String getProjectLogsPath(String projectName) {
		String logPath = DownloadUtils.getProjectConfigPath(projectName) + "logs"
				+ File.separator;
		return logPath;
	}
	
	
	public static String getProjectLogFile( String projectName, Date date )
	{
		String logPath = getProjectLogsPath(projectName);

		String year = DateFormatUtils.format(date, "yyyy");
		String month = DateFormatUtils.format(date, "MM");
		String day = DateFormatUtils.format(date, "dd");
		String hour = DateFormatUtils.format(date, "HH");
		String logFile = logPath + year + File.separator + month
				+ File.separator + day + File.separator + "download" + hour
				+ ".log";
		
		return logFile;
	}
	
	
	public static List<DownloadLog> fileToDownloadLogList( File file ) throws IOException, ParseException
	{
		if( !file.exists() || FileUtils.sizeOf(file) == 0 )
		{
			return null;
		}
		
		List<DownloadLog> retList = new ArrayList<DownloadLog>();
		
		LineIterator ite = FileUtils.lineIterator(file);
		while( ite.hasNext() )
		{
			String line = ite.nextLine();
			if( StringUtils.isNotBlank(line) )
			{
				retList.add(new DownloadLog(line));
			}			
		}
		
		return retList;
	}
	
	
	
	
	/**
	 * 下一个要保存的文件名 二层目录，二级目录最多1000，三级文件列表最多1000
	 * 该方法也同时为其他项目提供存储服务，spider参数可以为null
	 * project中必须有name
	 * 
	 * @param project
	 * @return
	 * @throws IOException
	 * @throws ConfigurationException
	 */
	public static File getSaveNextFile(String projectName, BaseSpider spider, boolean isSaveConfig)
			throws IOException, ConfigurationException {

		// 1.配置文件
		String configFileStr = DownloadUtils.getProjectConfigPath(projectName) + "config.properties";
		File configFile = new File(configFileStr);
		if (!configFile.exists()) {
			FileUtils.touch(configFile);
		}
		PropertiesConfiguration config = new PropertiesConfiguration(configFile);

		// 2.当前位置，如果没有，1/1/ 初始路径
		String currFilePath = config.getString("current.filepath", "1"
				+ File.separator + "1" + File.separator);

		// 3.计算下一个位置
		int level1 = NumberUtils.toInt(StringUtils.substringBefore(
				currFilePath, File.separator));
		int level2 = NumberUtils.toInt(StringUtils.substringBetween(
				currFilePath, File.separator));
		int pathFileCount = 1000;//以前是100
		// 添加根路径
		File currFilePathFile = new File(
				DownloadUtils.getProjectSavePath(projectName)
						+ currFilePath);
		if (!currFilePathFile.exists()) { // 不存在就新建
			FileUtils.forceMkdir(currFilePathFile);
		}

		if (ArrayUtils.getLength(currFilePathFile.list()) > pathFileCount)// 有可能包含两个子目录，不精确
		{
			level2++;

			if (level2 > pathFileCount)// 1001 上层+1
			{
				level1++;
				level2 = 1;
			}
		}
		String nextFilePath = level1 + File.separator + level2 + File.separator;
		
		if( isSaveConfig )
		{
			config.setProperty("current.filepath", nextFilePath);
			config.setProperty("current.url", spider!= null ? StringUtils.trimToEmpty(spider.getLastUrl()) : "");
			
			System.out.println( "保存属性文件：current.filepath=" + nextFilePath );
			System.out.println( "保存属性文件：current.url=" + spider!= null ? StringUtils.trimToEmpty(spider.getLastUrl()) : "" );
			
			config.save();
		}		

		// 4.返回值
		String fileName = RandomStringUtils.randomAlphanumeric(6);
		String retFile = DownloadUtils.getProjectSavePath(projectName)
				+ nextFilePath + fileName;

		return new File(retFile);
	}
	
	
	public static void updateConfigCurrentUrl( String projectName, String url )
	{
//		config.setProperty("current.url", spider!= null ? StringUtils.trimToEmpty(spider.getLastUrl()) : "");
//		
//		System.out.println( "保存属性文件：current.filepath=" + nextFilePath );
//		System.out.println( "保存属性文件：current.url=" + spider!= null ? StringUtils.trimToEmpty(spider.getLastUrl()) : "" );
	}
}
